package com.gzist.elderlyserviceplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.base.BaseDTO;
import com.gzist.elderlyserviceplatform.entity.dto.ListQueryPageDTO;
import com.gzist.elderlyserviceplatform.entity.dto.ServiceReservationDTO;
import com.gzist.elderlyserviceplatform.entity.po.ElderlyService;
import com.gzist.elderlyserviceplatform.entity.po.ServiceReservation;
import com.gzist.elderlyserviceplatform.mapper.ElderlyServiceMapper;
import com.gzist.elderlyserviceplatform.mapper.ServiceReservationMapper;
import com.gzist.elderlyserviceplatform.service.ElderlyServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzist.elderlyserviceplatform.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


/**
 * 养老服务表 服务实现类
 */
@Service
@RequiredArgsConstructor
public class ElderlyServiceServiceImpl extends ServiceImpl<ElderlyServiceMapper, ElderlyService> implements ElderlyServiceService {

    private final ServiceReservationMapper serviceReservationMapper;


    @Override
    public Page<ElderlyService> list(ListQueryPageDTO listQueryPageDTO) {
        LambdaQueryWrapper<ElderlyService> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ElderlyService::getName, listQueryPageDTO.getKeyWord());
        return this.page(new Page<>(listQueryPageDTO.getPageNum(), listQueryPageDTO.getPageSize()), wrapper);
    }

    @Override
    public void update(ElderlyService elderlyService) {
        LambdaUpdateWrapper<ElderlyService> wrapper = new UpdateWrapper<ElderlyService>().lambda();
        wrapper.eq(ElderlyService::getId, elderlyService.getId());
        Boolean b = this.update(elderlyService, wrapper);
        if (!b) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 分页查询养老服务信息
     * @return
     */
    @Override
    public Page<ElderlyService> queryPage(BaseDTO baseDTO) {
        // 执行分页查询，并返回查询结果
        return this.page(new Page<ElderlyService>(baseDTO.getPageNum(), baseDTO.getPageSize()));

    }

    @Override
    public void serviceReservation(ServiceReservationDTO elderlyService) {
        ServiceReservation serviceReservation = ServiceReservation.builder()
                .serviceId(elderlyService.getServiceId())
                .userId(elderlyService.getUserId())
                .userName(elderlyService.getUserName())
                .reservationTime(elderlyService.getReservationTime())
                .emergencyContactPhone(elderlyService.getEmergencyContactPhone())
                .serviceAddress(elderlyService.getServiceAddress())
                .build();
        serviceReservationMapper.insert(serviceReservation);
    }
}
